Skip to content

test: regression test for initialize hang on unexpected content-type#2472

Open
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/issue-2432
Open

test: regression test for initialize hang on unexpected content-type#2472
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/issue-2432

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

The underlying fix is already present on main_handle_post_request sends a proper JSONRPCError (with the matching request ID) when it encounters an unexpected content type, which lets ClientSession resolve the pending request instead of waiting indefinitely. This PR adds the missing test that exercises that code path for the initialize call specifically.

Test plan

  • tests/client/test_notification_response.py::test_initialize_does_not_hang_on_unexpected_content_type — new test passes
  • All existing test_notification_response.py tests continue to pass

Fixes #2432

…ype (modelcontextprotocol#2432)

Add a test that verifies initialize() raises MCPError immediately when
the server returns an unexpected Content-Type (e.g. text/plain) instead
of hanging forever waiting for a response that never arrives.

Fixes modelcontextprotocol#2432
@Christian-Sidak
Copy link
Copy Markdown
Author

Friendly bump -- let me know if anything needs changing.

1 similar comment
@Christian-Sidak
Copy link
Copy Markdown
Author

Friendly bump -- let me know if anything needs changing.

@MukundaKatta
Copy link
Copy Markdown

Does the same fix path also cover a server returning 200 with an empty body, or 200 with valid JSON for a different request id? Both have the same "initialize never resolves" shape, so if the fix is at the right layer they should unblock without new code. If those cases are tested elsewhere already, ignore.

@Christian-Sidak
Copy link
Copy Markdown
Author

No — this test is scoped to the unexpected content-type path only. The hang in issue #2432 is triggered when the SDK reaches the if content_type not in ("application/json", "text/event-stream") guard and raises McpError; the initialize() future is resolved by that error propagation.

The two cases you describe follow different paths:

  • 200 with empty body: content-type would still be application/json (or absent), so it bypasses the content-type check. The body JSON-parse would fail or produce an empty response, which is a separate code path — likely a different hang or error class.
  • 200 with valid JSON for a different request id: the response matcher would never complete the outstanding initialize future since the id doesn't match, so the session would hang differently — at the response-matching layer, not the content-type layer.

Both are valid "initialize never resolves" failure modes but would need separate tests against the relevant code paths. Happy to add them here or in a follow-up PR if that's useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Initialize call hangs forever if MCP server does not return a Content-Type: text/plain

2 participants